package cn.sf.sufinguserconsumer.controller;

import cn.sf.pojo.FileUploadResult;
import cn.sf.pojo.User;
import cn.sf.sufinguserconsumer.service.UserService;

import cn.sf.util.FileUpload;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@CrossOrigin("*")
public class UserController {
    @Resource
    private UserService userService;


    @Resource
    private FileUpload fileUpload;

    @RequestMapping("/updataById")
    public String updataById(User user) {
        System.out.println("传过来的name-------------->:" + user.getUserName());
        String userById = userService.updateUserById(user);
        return userById;
    }

    @RequestMapping("/queryRedisUserById")
    public User queryRedisUserById(Long id) {
        System.out.println("进来从redis中查询的方法");
        User redisUserById = userService.getRedisUserById(id);
        return redisUserById;
    }

    //登录
    @RequestMapping("/queryUser")
    public String queryUser(String userCode, String userPassword, HttpSession session) {
        System.out.println("userCode:" + userCode);
        User user = userService.queryUser(userCode, userPassword);
        if (user != null) {  //找到这个人
            session.setAttribute("user", user);
            return "登录成功";
        } else {  //没找打这个人
            return "登录失败";
        }

    }

    /**
     * 注册
     *
     * @param user
     * @param session
     * @return
     */
    @RequestMapping("/userAdd")
    public String userAdd(User user, HttpSession session) {
        System.out.println("name:" + user.getUserCode());
        int count = userService.userAdd(user);
        //班级添加学员成功
        if (count > 0) {
            session.setAttribute("user", user);
            return "到达注册页面";
        } else {
            return "到登录页面";
        }

    }

    @RequestMapping("/uploadHeda")
    public String uploadHead(@RequestParam("file") MultipartFile uploadFile, @RequestParam("userId") Long userId) throws Exception {
        System.out.println("文件:" + uploadFile);
        FileUploadResult upload = fileUpload.upload(uploadFile);
        System.out.println("文件在OSS的路径:" + upload.getName());
        //修改数据库路径
        String updateHead = userService.uploadHead(upload.getName(), userId);
        if (updateHead.equals("200")) {
            return "true";
        } else {
            return "false";
        }
    }

    //查询是否关注
    @RequestMapping("/queryIsOrNotAttention")
    public boolean queryIsOrNotAttention(long userId1, long userId2) {
        return userService.queryIsOrNotAttention(userId1, userId2);
    }

    //关注用户,取消关注
    @RequestMapping("/userAttention")
    public boolean userAttention(@RequestParam("userId1") long userId1,
                                 @RequestParam("userId2") long userId2) throws Exception {
        return userService.userAttention(userId1, userId2);
    }


}
